package top.lemna.code.generate;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import top.lemna.code.generate.util.JavaParserUtils;

/**
 * Dubbo生成器，会生成各种 dto bo mapper manager test
 * 
 * @author APPLE
 *
 */
@Slf4j
public class CommonGenerator {
  private final String className;

  private final String domainName;

  private final String templatePath;

  // 输出的包名，与 @see outPath 对应
  private final String basePackage;

  // 输出的路径
  private final String outPath;

  private final String artifactName;

  Map<String, Object> dataMap;

  public CommonGenerator(String className, String basePackage, String outPath,
      String templatePath) {
    this.className = className;
    this.templatePath = templatePath;
    this.outPath = outPath;
    this.basePackage = basePackage;
    this.artifactName = getArtifactName(basePackage);
    this.domainName = getDomainName(className);

  }


  public void generator() throws Exception {

    ClassElement classElement = JavaParserUtils.parserJava(className);
    List<JavaField> list = classElement.getFieldList();
    String classComment = classElement.getClassComment().replaceAll("<br>", "");

    dataMap = new HashMap<String, Object>();
    dataMap.put("classPath", basePackage);
    dataMap.put("classComment", classComment);
    dataMap.put("domain", domainName);
    dataMap.put("fieldList", list);
    dataMap.put("artifact", artifactName);

    File file = new File(templatePath);
    Consumer<File> consumer = f -> generator(f);
    FileUtil.walkFiles(file, consumer);
  }

  void generator(File file) {

    System.out.println(file.getAbsolutePath());
    String templateName = file.getAbsolutePath().replaceAll("\\\\", "/");
    templateName = templateName.replaceAll(templatePath, "");
    System.out.println("templateName:" + templateName);

    String outFilePath = outPath;
    if (templateName.contains("/")) {
      outFilePath = outPath + templateName.substring(0, templateName.lastIndexOf("/")) + "/";
    }
    System.out.println("outFilePath:" + outFilePath);


    String outFileName = templateName;
    if (templateName.contains("/")) {
      outFileName = templateName.substring(templateName.lastIndexOf("/")+1);
    }


    outFileName = domainName + outFileName;
    System.out.println("outFileName:" + outFileName);

    try {
      generator(templateName, outFilePath, outFileName);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private void generator(//
      String templateName, //
      String outFilePath, //
      String outFileName//
  ) throws Exception {
    FreemarkerGenerator fg = new FreemarkerGenerator(templatePath);
    fg.generator(dataMap, templateName, outFilePath, outFileName);
  }


  static String getDomainName(String className) {
    return className.replaceAll("\\\\", "/")
        .substring(className.lastIndexOf("/"), className.lastIndexOf(".")).replaceAll("/", "");
  }

  static String getArtifactName(String basePackage) {
    return basePackage.substring(basePackage.lastIndexOf(".") + 1);
  }
}
